;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                           ;;
;;                     Unofficial Chinese Clone Support                      ;;
;;                      http://code.google.com/p/uccs/                       ;;
;;                                                                           ;;
;;                            Version 0.21                                   ;;
;;                                                                           ;;
;;                            by Tomas Rimkus                                ;;
;;             (based on work by Brian Jorgensen, Leon and Micha)            ;;
;;                                                                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Menu, TRAY, Icon, UCCS.ico
Menu, TRAY, Tip, Unofficial Chinese Clone Support


; There used to be separate sections, but it
; was inconvenient for writing settings back
; out. If we get many more settings, we need
; to find a way to put sections back in.

ConfigFilename = UCCS.ini
SectionName = UCCS

;;
;; Behavioral Settings
;;

IniRead, SwapWinAndAlt, %ConfigFilename%, %SectionName%, SwapWinAndAlt, 0
IniRead, RemapCapsLockToCtrl, %ConfigFilename%, %SectionName%, RemapCapsLockToCtrl, 0
IniRead, RemapCapsLockToShift, %ConfigFilename%, %SectionName%, RemapCapsLockToShift, 0
IniRead, RemapRightWinToFn, %ConfigFilename%, %SectionName%, RemapRightWinToFn, 0
IniRead, RemapRightWinToCtrl, %ConfigFilename%, %SectionName%, RemapRightWinToCtrl, 0
IniRead, RemapAltBackquote, %ConfigFilename%, %SectionName%, RemapAltBackquote, 0
IniRead, RectangleEjects, %ConfigFilename%, %SectionName%, RectangleEjects, 0

;;
;; Media Key Settings
;;

IniRead, EjectDelayTime, %ConfigFilename%, %SectionName%, EjectDelayTime, 500

IniRead, VolumeControlsAreDefault, %ConfigFilename%, %SectionName%, VolumeControlsAreDefault, 0
IniRead, MediaControlsAreDefault, %ConfigFilename%, %SectionName%, MediaControlsAreDefault, 0

IniRead, SyncWaveVolumeToMasterVolume, %ConfigFilename%, %SectionName%, SyncWaveVolumeToMasterVolume, 0

IniRead, VolumeChangeRate, %ConfigFilename%, %SectionName%, VolumeChangeRate, 2.0
VolumeDownRate := VolumeChangeRate
VolumeUpRate   := VolumeChangeRate
IniRead, VolumeDownRate, %ConfigFilename%, %SectionName%, VolumeDownRate, %VolumeDownRate% 
IniRead, VolumeUpRate, %ConfigFilename%, %SectionName%, VolumeUpRate, %VolumeUpRate% 

IniRead, OverlayDisplayTime, %ConfigFilename%, %SectionName%, OverlayDisplayTime, 1200
IniRead, OverlayDisplayCentered, %ConfigFilename%, %SectionName%, OverlayDisplayCentered, 1
IniRead, OverlayWidth, %ConfigFilename%, %SectionName%, OverlayWidth, 200
IniRead, OverlayHeight, %ConfigFilename%, %SectionName%, OverlayHeight, 30
IniRead, OverlayTransparency, %ConfigFilename%, %SectionName%, OverlayTransparency, 180

IniRead, OptimizeForWindowsVista, %ConfigFilename%, %SectionName%, OptimizeForWindowsVista, 1

VolumeColorBg                := "d0f8d0"
VolumeColorBar               := "44ff44"
MutedVolumeColorBg           := "f8d0d0"
MutedVolumeColorBar          := "f03333"

;; ignore errors about "menu not found", etc.
;Menu, TRAY, UseErrorLevel, On

InitPreference(name) {
	SetPreference(name, %name%)
}

TogglePreference(name) {
	SetPreference(name, !%name%)
	
	if (name = "RemapCapsLockToCtrl" && %name%)
		SetPreference("RemapCapsLockToShift", false)
	else if (name = "RemapCapsLockToShift" && %name%)
		SetPreference("RemapCapsLockToCtrl", false)
	if (name = "RemapRightWinToFn" && %name%)
		SetPreference("RemapRightWinToCtrl", false)
	else if (name = "RemapRightWinToCtrl" && %name%)
		SetPreference("RemapRightWinToFn", false)
}

SetPreference(name, newValue) {
	global
	SetEnv, %name%, %newValue%
	MenuText := %name%Text

	if (newValue) {
		menu, TRAY, Check, %MenuText%
	} else {
		menu, TRAY, Uncheck, %MenuText%
	}
	
	if (name = "RemapCapsLockToShift" || name = "RemapCapsLockToCtrl") {
		if (newValue) {
			SetCapsLockState, AlwaysOff
		} else {
			SetCapsLockState, Off
		}
	}

	IniWrite, %newValue%, %ConfigFilename%, %SectionName%, %name%
	
	;RAlt will work as expected if it's not swapped
	if (name = "SwapWinAndAlt")
	{
		if (newValue) 
		{
			Hotkey $*RAlt, On, UseErrorLevel
			Hotkey $*RAlt Up, On, UseErrorLevel
		} 
		else 
		{
			Hotkey $*RAlt, Off, UseErrorLevel
			Hotkey $*RAlt Up, Off, UseErrorLevel
		}
	}
}


;;
;; Notification Icon Menu
;;

SwapWinAndAltText                := "Swap Command and Option keys"
RemapCapsLockToCtrlText          := "Use caps lock as an extra Ctrl key"
RemapCapsLockToShiftText         := "Use caps lock as an extra shift key"
RemapRightWinToFnText            := "Use right command as an extra fn key"
RemapRightWinToCtrlText          := "Use right command as an extra Ctrl key"
RemapAltBackquoteText            := "Use Alt-`` as alt-shift-tab"
RectangleEjectsText              := "Rounded rectangle key ejects cd-rom"
;;                               ---------------------------------------------
MediaControlsAreDefaultText      := "Media keys work without holding fn"
VolumeControlsAreDefaultText     := "Volume keys work without holding fn"
SyncWaveVolumeToMasterVolumeText := "Volume changes affect wave device"
;;
OptimizeForWindowsVistaText      := "I use Windows Vista/7 (or disable overlays)"

Menu, TRAY, NoMainWindow
Menu, TRAY, NoStandard

Menu, TRAY, add, %OptimizeForWindowsVistaText%, OptimizeForWindowsVistaMenuHandler
Menu, TRAY, add, %RectangleEjectsText%, RectangleEjectsHandler
Menu, TRAY, add,,
Menu, TRAY, add, %SwapWinAndAltText%, SwapWinAndAltHandler
Menu, TRAY, add, %RemapAltBackquoteText%, RemapAltBackquoteHandler
Menu, TRAY, add, %RemapRightWinToCtrlText%, RemapRightWinToCtrlHandler
Menu, TRAY, add, %RemapRightWinToFnText%, RemapRightWinToFnHandler
Menu, TRAY, add, %RemapCapsLockToCtrlText%, RemapCapsLockToCtrlHandler
Menu, TRAY, add, %RemapCapsLockToShiftText%, RemapCapsLockToShiftHandler
Menu, TRAY, add,,
Menu, TRAY, add, %MediaControlsAreDefaultText%, MediaControlsAreDefaultMenuHandler
Menu, TRAY, add, %VolumeControlsAreDefaultText%, VolumeControlsAreDefaultMenuHandler
Menu, TRAY, add, %SyncWaveVolumeToMasterVolumeText%, SyncWaveVolumeToMasterVolumeMenuHandler
Menu, TRAY, add,,
Menu, TRAY, add, Exit, MenuQuit
Menu, TRAY, add, Restart UCCS, MenuRestart
Menu, TRAY, Default, Restart UCCS

Goto SkipHandlers
MenuQuit:
	ExitApp
	return
MenuRestart:
	Reload
	return
RectangleEjectsHandler:
	TogglePreference("RectangleEjects")
	return
SwapWinAndAltHandler:
	TogglePreference("SwapWinAndAlt")
	return
RemapCapsLockToCtrlHandler:
	TogglePreference("RemapCapsLockToCtrl")
	return
RemapCapsLockToShiftHandler:
	TogglePreference("RemapCapsLockToShift")
	return
RemapRightWinToFnHandler:
	TogglePreference("RemapRightWinToFn")
	return
RemapRightWinToCtrlHandler:
	TogglePreference("RemapRightWinToCtrl")
	return
RemapAltBackquoteHandler:
	TogglePreference("RemapAltBackquote")
	return
MediaControlsAreDefaultMenuHandler:
	TogglePreference("MediaControlsAreDefault")
	return
VolumeControlsAreDefaultMenuHandler:
	TogglePreference("VolumeControlsAreDefault")
	return
SyncWaveVolumeToMasterVolumeMenuHandler:
	TogglePreference("SyncWaveVolumeToMasterVolume")
	return
OptimizeForWindowsVistaMenuHandler:
	TogglePreference("OptimizeForWindowsVista")
	return
SkipHandlers:

DetectHiddenWindows, On
SetCapsLockState, Off

InitPreference("OptimizeForWindowsVista")
InitPreference("RectangleEjects")

InitPreference("SwapWinAndAlt")
InitPreference("RemapAltBackquote")
InitPreference("RemapRightWinToFn")
InitPreference("RemapRightWinToCtrl")
InitPreference("RemapCapsLockToShift")
InitPreference("RemapCapsLockToCtrl")
InitPreference("MediaControlsAreDefault")
InitPreference("VolumeControlsAreDefault")
InitPreference("SyncWaveVolumeToMasterVolume")
